package com.cms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.ReportConstant;
import com.cms.service.ReportService;
import com.google.gson.Gson;

@Controller
public class PlatformStatisticsController extends BaseController{

	  @Autowired
	  private ReportService reportService;
	  
	  @RequestMapping(value="/platformProfitanloss/platformProfitanlossChart.do")
	  public String platformProfitanlossChart(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
	  { 
		   return "platform/platformProfitanlossChart.html";
	  }
	  
	  @RequestMapping(value="/platformProfitanloss/platformProfitanlossChartData.do")
	  public void platformProfitanlossChartData(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
	  { 
		  Map<String,Object>result =new HashMap<String, Object>();
		   String statisticsType = req.getParameter("statisticsType");
		   Map<String,Object>queryMap = this.buildQueryMap(req);
		   List<Map>dataList = null;
		   if(StringUtils.equals(statisticsType, ReportConstant.STATISTICS_TYPE_DAY))
		   {
			   dataList=this.reportService.getReportResult("platformProfitanlossDay", queryMap); 
		   }
		   else if(StringUtils.equals(statisticsType, ReportConstant.STATISTICS_TYPE_MONTH))
		   {
			   dataList=this.reportService.getReportResult("platformProfitanlossMonth", queryMap);
		   }
		   StringBuilder sb = new StringBuilder();
		   sb.append(queryMap.get("startDate"));
		   sb.append("-");
		   sb.append(queryMap.get("endDate"));
		   result.put("subtitle", sb.toString());
		   fillDataIntoChart(dataList, result);
           this.renderJson(response, new Gson().toJson(result));
	  }
	  
	  /***
	   * 
	   * 将数据格式填充到报表数据格式
	   * @param dataList
	   * @param result
	   */
	  private void fillDataIntoChart( List<Map>dataList,Map<String,Object>result)throws Exception
	  {
		  if(dataList!=null&&!dataList.isEmpty())
		  {
			  Set<String>categories = new LinkedHashSet<String>();
			  List<Map<String, Object>>series = new ArrayList<Map<String,Object>>();
			  Map<String,Object>betsAmount=new HashMap<String, Object>();
			  betsAmount.put("name", "投注金额");
			  betsAmount.put("data", new ArrayList());
			  Map<String,Object>chargeAmount=new HashMap<String,Object>();
			  chargeAmount.put("name", "充值金额");
			  chargeAmount.put("data", new ArrayList());
			  Map<String,Object>cashAmount=new HashMap<String, Object>();
			  cashAmount.put("name", "提现金额");
			  cashAmount.put("data", new ArrayList());
			  Map<String,Object>balanceAmount=new HashMap<String, Object>();
			  balanceAmount.put("name", "平台余额");
			  balanceAmount.put("data", new ArrayList());
			  Map<String,Object>profitLossAmount=new HashMap<String, Object>();
			  profitLossAmount.put("name", "盈亏金额");
			  profitLossAmount.put("data", new ArrayList());
			  for(Map<String,Object>data:dataList)
			  {
				  categories.add(StringUtils.obj2String(data.get("executeDate")));
				  ((List)betsAmount.get("data")).add(data.get("betsAmount"));
				  ((List)chargeAmount.get("data")).add(data.get("charge"));
				  ((List)cashAmount.get("data")).add(data.get("cash"));
				  ((List)balanceAmount.get("data")).add(data.get("balance"));
				  ((List)profitLossAmount.get("data")).add(data.get("profitLoss"));
			  }
			  series.add(betsAmount);
			  series.add(chargeAmount);
			  series.add(cashAmount);
			  series.add(balanceAmount);
			  series.add(profitLossAmount);
			  result.put("categories", categories);
			  result.put("series", series);
		  }
	  }
	  
	  
	  @RequestMapping(value="/platformProfitanloss/platformProfitanloss.do")
	  public String platformProfitanloss(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
	  {
		   String statisticsType = req.getParameter("statisticsType");
		   Map<String,Object>queryMap = this.buildQueryMap(req);
		   if(StringUtils.equals(statisticsType, ReportConstant.STATISTICS_TYPE_DAY))
		   {
			   map.put("platformProfitanlossList", this.reportService.pagingReportResult("platformProfitanlossDay", queryMap));
		   }else if(StringUtils.equals(statisticsType, ReportConstant.STATISTICS_TYPE_MONTH))
		   {
			   map.put("platformProfitanlossList", this.reportService.pagingReportResult("platformProfitanlossMonth", queryMap));
		   }
		   return "platform/platformProfitanlossList.html";
	  }
	  
	  @RequestMapping(value="/platformProfitanloss.do")
	  public String platform(HttpServletRequest req,HttpServletResponse response,ModelMap map)throws Exception
	  {
		   return "platform/platformProfitanlossMain.html";
	  }
	  
	  
	 
	
}
